﻿using Sys.Common;
using Sys.Core.Dri;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Api.Dri
{
    public class TeacherApi : BaseApi
    {
        public string Grid(int rows, int page)
        {
            var entities = TeacherBll.I.SortEntities;

            var list = TeacherBll.I.EntitiesPage(entities, rows, page);

            return GetGrid(entities.Count(), list);
        }

        public string ComboBoxList()
        {
            var entities = TeacherBll.I.SortEntities;
            List<JsonMap> jms = new List<JsonMap>();
            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            return CustomJson(entities.ToList(), jms, false);
        }

        public string Save(DriTeacher model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                //model.IntSort = ButtonBll.I.MaxSort(m => m.IntSort);
                model.Num = TeacherBll.I.NewNum;  //再次获取流水号，避免并发影响
                model.InsertTime = DateTime.Now;
                model.UpdateTime = model.InsertTime;
                model.IsVisible = true;  //默认开启
                //if (String.IsNullOrEmpty(model.Photo))
                //    model.Photo = "null.png";
                result.Success = TeacherBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                model.UpdateTime = DateTime.Now;
                //if (String.IsNullOrEmpty(model.Photo))
                //    model.Photo = "null.png";
                result.Success = TeacherBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = TeacherBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            DriTeacher model = TeacherBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = TeacherBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

        /// <summary>
        /// 添加相片
        /// </summary>
        /// <returns></returns>
        public string AddPhoto()
        {
            Hashtable hash = KindHelper.ImgUpload();

            if (Convert.ToInt32(hash["error"]) == 0)
            {
                HttpContext context = HttpContext.Current;

                int w = Convert.ToInt32(context.Request.QueryString["w"]);
                int h = Convert.ToInt32(context.Request.QueryString["h"]);

                string filename = hash["url"].ToString();

                string temppath = context.Server.MapPath("~/userfiles/temp/" + filename);
                string filepath = context.Server.MapPath("~/userfiles/teacher/");

                if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); }

                ThumNail.MakeThumNail(temppath, filepath + filename, w, h, TnType.Cut);

                System.IO.File.Delete(temppath);

                hash["url"] = filename;
            }

            return JsonHelper.ToJson(hash);
        }

        /// <summary>
        /// 导出
        /// </summary>
        public void Export()
        {
            var entities = TeacherBll.I.SortEntities;
            var columns = new List<string[]>();
            columns.Add(new string[] { "Num", "教练编号" });
            columns.Add(new string[] { "Name", "姓名" });
            columns.Add(new string[] { "Sex", "性别" });
            columns.Add(new string[] { "CertificateType", "证件类型" });
            columns.Add(new string[] { "CertificateNum", "证件编号" });
            columns.Add(new string[] { "SignUpDate", "加入时间" });
            columns.Add(new string[] { "DivingNum", "驾照编号" });
            columns.Add(new string[] { "DivingType", "准驾车型" });
            columns.Add(new string[] { "DivingExpDate", "驾驶证有效期" });
            columns.Add(new string[] { "TeacherNum", "教练证号" });
            columns.Add(new string[] { "TeacherCarType", "准教车型" });
            columns.Add(new string[] { "TeacherExpDate", "教练证有效期" });
            columns.Add(new string[] { "TeacherType", "教练类型" });
            columns.Add(new string[] { "Mobile", "手机" });
            columns.Add(new string[] { "Address", "住址" });
            columns.Add(new string[] { "IsVisible", "启用" });
            NopiHelper.Export(columns, entities.ToList());
        }
    }
}
